'''
Created on May 8, 2012

@author: lavang
'''
from Interface.professorPage import xpathLessonInformation, xpathContent
from Interface.lessonInformationPage import xpathEditInformation,\
    xpathBtnAddDescription, xpathTxtDescription, xpathBtnSaveDescription,\
    xpathLinkLessonInformation, xpathEditRules, xpathLinkAddCompletionRule,\
    xpathCbbCondition, xpathCbbRelation, xpathTxtPercentage,\
    xpathCbbSpecificUnit, xpathBtnSubmitRule
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support.select import Select
from selenium import webdriver

# click on lesson
def clickOnLesson(self,lessonName):
    browser=self.browser
    element=browser.find_element_by_xpath("//a[contains(text(),'"+lessonName+"')]")
    element.click()
    
#click on Lesson Information
def clickOnLessonInformation(self):
    browser=self.browser
    element=browser.find_element_by_xpath(xpathLessonInformation())
    element.click()
    
# click on Edit information
def clickOnEditInformation(self):
    browser=self.browser
    element=browser.find_element_by_xpath(xpathEditInformation())
    element.click()
    
# click on Add description
def clickOnAddDescription(self):
    browser=self.browser
    element=browser.find_element_by_xpath(xpathBtnAddDescription())
    element.click()
    
# input description
def inputDescription(self,content):
    browser=self.browser
    element=browser.find_element_by_xpath(xpathTxtDescription())
    element.clear()
    element.send_keys(content)
    element=browser.find_element_by_xpath(xpathBtnSaveDescription())
    element.click()
    
# back to Lesson information
def clickOnLinkLessonInformation(self):
    browser=self.browser
    element=browser.find_element_by_xpath(xpathLinkLessonInformation())
    element.click()
    
# Add description
def verifyDescriptionExist(self,description):
    browser=self.browser
    try:
        WebDriverWait(browser,20).until(lambda browser : browser.find_element_by_xpath("//td[text()='"+description+"']"))
        return True
    except:
        return False
    
# click on Edit rule
def clickOnEditRules(self):
    browser=self.browser
    element=browser.find_element_by_xpath(xpathEditRules())
    element.click()
    
# Add completion rule
def clickOnAddCompletionRule(self):
    browser=self.browser
    element=browser.find_element_by_xpath(xpathLinkAddCompletionRule())
    element.click()
    
# Add rule
def addRule(self,rule,relation,percentage,unit,edit='on'):
    browser=self.browser
    if edit=='on':
        pass
    else:
    # rule
        element=browser.find_element_by_xpath(xpathCbbCondition())
        Select(element).select_by_visible_text(rule)
    
    # relation
    element=browser.find_element_by_xpath(xpathCbbRelation())
    Select(element).select_by_visible_text(relation)
    
    if relation=='Seen a fraction of units':
        element=browser.find_element_by_xpath(xpathTxtPercentage())
        element.clear()
        element.send_keys(percentage)
    else:
        element=browser.find_element_by_xpath(xpathCbbSpecificUnit())
        Select(element).select_by_visible_text(unit)
    # submit
    element=browser.find_element_by_xpath(xpathBtnSubmitRule())
    element.click()
    
# verify rule is added
def isRuleAddedSuccessfully(self,rule):
    browser=self.browser
    try:
        WebDriverWait(browser,20).until(lambda browser : browser.find_element_by_xpath("//td[text()='"+rule+"']"))
        return True
    except:
        return False
    
# verify rule is edited
def isRuleEditedSuccessfully(self,rule,relation):
    browser=self.browser
    element=browser.find_element_by_xpath("//td[text()='"+rule+"']/../td[3]")
    text=element.text
    if text==relation:
        return True
    else:
        return False  

# click on Edit button
def clickOnEditButton(self,rule):
    browser=self.browser
    element=browser.find_element_by_xpath("//td[text()='"+rule+"']/../td[4]/a")
    element.click()
    
# delete rule
def deleteRule(self,rule):
    browser=self.browser
    element=browser.find_element_by_xpath("//td[text()='"+rule+"']/../td[4]/img")
    element.click()
    alert=browser.switch_to_alert()
    alert.accept()
    browser.refresh()
    
# verify rule is Delete
def isRuleDeletedSuccessfully(self,rule):
    browser=self.browser
    try:
        WebDriverWait(browser,20).until(lambda browser : browser.find_element_by_xpath("//td[text()='"+rule+"']"))
        return True
    except:
        return False
    
# click on Content button
def clickOnContentButton(self):
#    browser = webdriver.Firefox()
    browser=self.browser
    element = browser.find_element_by_xpath(xpathContent())
    element.click()